package com.globallogic.vkulyk.SSHTool;

import static org.junit.Assert.*;
import org.junit.*;
import com.jcraft.jsch.Session;
import java.io.*;

public class Unit_CommandExecutor extends SSHToolTester {
	private static final String OUTPUT_NOT_EQUALS = "Output not equals";
	private final ByteArrayOutputStream outActual = new ByteArrayOutputStream();

	@Before
	public void setup() {
		setVarsFromPropertiesFile("testData/testing.properties");
		System.setOut(new PrintStream(outActual));
	}

	@Test
	public void shouldExecuteCommand_whenCallExecute() {
		// given
		String outExpected = "Linux";

		// when
		new CommandExecutor(host, port, user, password, command).execute();
		String outActualWithRemovedNewLine = outActual.toString().replaceAll(
				"\\n", "");

		// then
		assertEquals(OUTPUT_NOT_EQUALS, outExpected,
				outActualWithRemovedNewLine);
	}

	@Test
	public void shouldExecuteCommand_whenCallExecuteInExistingSession() {
		// given
		ActionExecutor actionExecutor = new ActionExecutor(host, port, user,
				password);
		Session session = actionExecutor.openSession();
		String outExpected = "Linux";

		// when
		new CommandExecutor(host, port, user, password, command)
				.executeInExistingSession(session);
		String outActualWithRemovedNewLine = outActual.toString().replaceAll(
				"\\n", "");

		// then
		assertEquals(OUTPUT_NOT_EQUALS, outExpected,
				outActualWithRemovedNewLine);

		actionExecutor.closeSession(session);
	}

	@After
	public void cleanup() {
		System.setOut(null);
	}

}
